﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.SqlClient;
using System.Data;

public partial class create_folder_images : System.Web.UI.Page
{
    SqlCommand cmd;
    SqlDataReader dr;
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime dt = DateTime.Now;
        Response.Write( DateTime.Now.ToString("yyyyMM"));

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (!Directory.Exists(MapPath("~/images/201301")))
        {
            Directory.CreateDirectory(MapPath("/images/201301"));
            Response.Write("Tạo thư mục thành công!");

            List<ObjTour> lst = getAllTour();
            int i = 1;
            foreach (ObjTour item in lst)
            {
                if (item.Anh_to == item.Anh_nho)
                {

                }
                else
                {
                    if (File.Exists(MapPath("~/images/") + item.Anh_nho))
                    {

                        File.Move(MapPath("~/images/") + item.Anh_nho, MapPath("~/images/201301/") + i + Path.GetExtension(MapPath("~/images/") + item.Anh_nho));
                        item.Anh_nho = "201301/" + i + Path.GetExtension(MapPath("~/images/") + item.Anh_nho);
                    }
                    if (File.Exists(MapPath("~/images/") + item.Anh_to))
                    {

                        File.Move(MapPath("~/images/") + item.Anh_to, MapPath("~/images/201301/") + i + "_1" + Path.GetExtension(MapPath("~/images/") + item.Anh_to));
                        item.Anh_to = "201301/" + i + "_1" + Path.GetExtension(MapPath("~/images/") + item.Anh_to);
                    }
                    updateanh(item.Tour_id, item.Anh_nho, item.Anh_to);
                }
                i++;
            }
        }

    }
    public List<ObjTour> getAllTour()
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_TourGetAll", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
       
        dr = cmd.ExecuteReader();
        ObjTour objTour;
        List<ObjTour> lst = new List<ObjTour>();
        while (dr.Read())
        {
            objTour = new ObjTour();
            objTour.Tour_id = int.Parse(dr["Tour_id"].ToString());
            
            objTour.Anh_nho = dr["anh_nho"].ToString();
            objTour.Anh_to = dr["anh_to"].ToString();
            lst.Add(objTour);
        }
        dr.Close();
        sqlConnection.Close();
        return lst;
    }
    public void updateanh(int id,string anh_nho, string anh_to)
    {
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        SqlConnection sqlConnection = new SqlConnection(constr);

        sqlConnection.Open();
        cmd = new SqlCommand("sp_TourUpdateAnh", sqlConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter[] sqla = new SqlParameter[3];
        sqla[0] = new SqlParameter("Tour_id", DbType.Int32);
        sqla[1] = new SqlParameter("anh_nho", DbType.String);
        sqla[2] = new SqlParameter("anh_to", DbType.String);

        sqla[0].Value = id;
        sqla[1].Value = anh_nho;
        sqla[2].Value = anh_to;
        cmd.Parameters.AddRange(sqla);
        cmd.ExecuteNonQuery();
        sqlConnection.Close();
    }

}